package com.xframe.xdal.core.serialize;



import com.alibaba.fastjson.JSONArray;
import com.xframe.xdal.core.util.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.*;

@Slf4j
public class JsonConfigContainer {

    // 存放文件的基础路径
    private String basePath;
    private Map<String, JsonConfig> jsonConfigMap;

    private JsonConfigContainer(String basePath,Map<String,JsonConfig> jsonConfigMap){
        this.basePath = basePath;
        this.jsonConfigMap = jsonConfigMap;
    }

    private JsonConfigContainer(){
        this.basePath = "";
        this.jsonConfigMap = new HashMap<>();
    }

    public static final String defaultQueryKey = "default";
    public static final String defaultVer = "v1";
    public static final String defaultLanguage = "cn";

    public static void init(String basePath){
        // 在这里读取文件 初始化 jsonConfigMap
        Map<String,JsonConfig> jsonConfigMap = procFiles(basePath);
        //查询数据库中的配置
//        FindOptions options = new FindOptions();
//        options.getConditionValueCollection().add(new ConditionValue("delFlag",0));
//        List<SysModelConfig> modelViewList = DataBaseAccess.getInstance().findAll(SysModelConfig.class,options);
//        jsonConfigMap = procView(modelViewList, jsonConfigMap);
        configContainer = new JsonConfigContainer(basePath,jsonConfigMap);


    }

    public static void init(){
        configContainer = new JsonConfigContainer();
    }

    private static JsonConfigContainer configContainer = null;


    public void addConfig(IModelConfig sysModelConfig){
        String language = sysModelConfig.getLanguage();
        String ver = sysModelConfig.getVer();
        String queryKey = sysModelConfig.getQueryKey();
        String modelCode = sysModelConfig.getModelCode()+".json";
        String content = sysModelConfig.getContent();
        List<JsonModel> jsonModelList = JSONArray.parseArray(content, JsonModel.class);
        JsonConfig jsonConfig = new JsonConfig(language,ver,modelCode,queryKey,jsonModelList);
        jsonConfigMap.put(jsonConfig.getKey(),jsonConfig);
    }

    public void addConfigByNoContainsKey(IModelConfig sysModelConfig){
        String language = sysModelConfig.getLanguage();
        String ver = sysModelConfig.getVer();
        String queryKey = sysModelConfig.getQueryKey();
        String modelCode = sysModelConfig.getModelCode()+".json";
        String content = sysModelConfig.getContent();
        List<JsonModel> jsonModelList = JSONArray.parseArray(content, JsonModel.class);
        JsonConfig jsonConfig = new JsonConfig(language,ver,modelCode,queryKey,jsonModelList);
        if(!jsonConfigMap.containsKey(jsonConfig.getKey())){
            jsonConfigMap.put(jsonConfig.getKey(),jsonConfig);
        }

    }

    public static JsonConfigContainer getConfigContainer(){
        return configContainer;
    }


    public String getBasePath() {
        return basePath;
    }

    public JsonConfig getJsonConfig(String language, String ver, String code, String queryKey){
        return getJsonConfigHelp(language,ver,queryKey,code);
    }

    public static String getKey(String language,String ver,String code,String queryKey){
        return (language + "." + ver + "." + code + "." + queryKey).toLowerCase();
    }

    private JsonConfig getJsonConfigHelp(String language,String ver,String queryKey,String code){
        String key = getKey(language,ver,code,queryKey);

        JsonConfig jsonConfig = jsonConfigMap.get(key);

        jsonConfig = jsonConfig != null ? jsonConfig : jsonConfigMap.get(getKey(defaultLanguage,defaultVer,code,defaultQueryKey));
        return jsonConfig;
    }


    private static Map<String,JsonConfig> procFiles(String basePath){
        // 获得语言文件夹名称
        List<String> langList = FileUtil.readDirectoryName(basePath);
        Map<String,JsonConfig> jsonConfigMap = new HashMap<>(64);
        for(int i = 0; i < langList.size(); i++){
            String language = langList.get(i);
            String langPath = basePath + language + File.separator;
            List<String> vers = FileUtil.readDirectoryName(langPath);

            for(int j = 0; j < vers.size(); j++){
                String ver = vers.get(j);
                String verPath = langPath + ver + File.separator;
                List<String> fileList = FileUtil.readFileName(verPath);
                log.info("fileList={}",fileList);
                fileList.forEach(fileName -> {
                    Map map = FileUtil.readFileGetJson(basePath,language,ver,fileName);

                    Set<Map.Entry<String,Object>> ent =  map.entrySet();
                    for(Map.Entry<String,Object> entry:ent){

                        String queryKey = entry.getKey();
                        List<JsonModel> jsonModelList = JSONArray.parseArray(entry.getValue().toString(), JsonModel.class);

                        JsonConfig jsonConfig = new JsonConfig(language,ver,fileName,queryKey,jsonModelList);
                        jsonConfigMap.put(jsonConfig.getKey(),jsonConfig);
                    }
                });
            }
        }
        return jsonConfigMap;

    }


}
